package com.itheima.ydd.controller;


import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 菜单表
 */
@Slf4j
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 所有菜单,列出所有菜单信息
     *
     * @return
     */
    @GetMapping("/allData")
    public ResultData allData() {

        return sysMenuService.allData();

    }

    /**
     * 回显菜单信息
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        //根据id查找菜单信息
        SysMenu byId = sysMenuService.getById(id);
        return ResultData.ok("", byId);
    }

    /**
     * 更新菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody SysMenu sysMenu) {
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenuService.updateById(sysMenu);
        return ResultData.ok("修改成功", "");

    }

    /**
     * 添加菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return ResultData.ok("添加成功", "");

    }

    /**
     * 根据id删除菜单信息
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {
        sysMenuService.del(id);
        return ResultData.ok("删除成功", "");
    }
}
